﻿using NUnit.Framework;
using System.Collections.Generic;
using OpenSda.Agilcont.Common.Data.Contracts;
using OpenSda.Agilcont.Common.Data.Entities;

namespace OpenSda.Agilcont.Common.Data.NhContexts.NuTest
{
    /// <summary>
    /// This class contains tests for the DAO implementations
    /// </summary>
    /// <author>Clt K D</author>
    [TestFixture]
    public class CustomerDataTest : AbstractBaseDataTests
    {
        private ICustomerData customerData;
        //private IOrderDao orderDao;




        // These properties will be injected based on type
        public ICustomerData CustomerData
        {
            set { customerData = value; }
        }


        //public IOrderDao OrderDao
        //{
        //    set { orderDao = value; }
        //}


        //protected ISessionFactory sessionFactory;
        //public ISessionFactory SessionFactory
        //{
        //    set { sessionFactory = value; }
        //}

        [Test()]
        public void MiFunTest()
        {

            string actual = customerData.MiFun();
            Assert.IsNotNull(actual, "customer dao is null");
        }


        [Test()]
        public void SanityCheck()
        {

            Assert.IsNotNull(customerData, "customer dao is null");

        }
        [Test]
        public void CustomerDaoList2Tests()
        {
            var x = customerData.GetAll2();
            Assert.IsNotNull(x);
        }

        [Test]
        public void CustomerDaoSeclectTests()
        {
            Customer c = customerData.Select("AS5");
            Assert.IsNotNull(c);
        }

        [Test]
        public void CustomerDaListAllTests()
        {
            var c = customerData.GetAll();

            Assert.IsNotNull(c);
        }

        [Test]
        public void CustomerDeleteTests()
        {
            Customer c = customerData.Select("AS2");
            customerData.Delete(c);
        }

        [Test]
        public void CustomerDaoTests()
        {
            //DefaultRollback = false;
            //Assert.AreEqual(91, customerDao.GetAll().Count);
            //sessionFactory.GetCurrentSession().BeginTransaction();
            Customer c = new Customer();
            c.Id = "AS5";
            c.CompanyName = "UPeU-5";
            customerData.Save(c);
            //sessionFactory.GetCurrentSession().BeginTransaction().Commit();
            //customerDao.Update(c);
            //c = customerDao.Get("MPOLL");
            //Assert.AreEqual(c.Id, "MPOLL");
            //Assert.AreEqual(c.CompanyName, "Interface21");

            ////Without flushing, nothing changes in the database:
            //int customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers");
            //Assert.AreEqual(91, customerCount);

            ////Flush the session to execute sql in the db.
            //SessionFactoryUtils.GetSession(sessionFactory, true).Flush();

            ////Now changes are visible outside the session but within the same database transaction
            //customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers");
            //Assert.AreEqual(92, customerCount);

            //Assert.AreEqual(92, customerDao.GetAll().Count);

            //c.CompanyName = "SpringSource";

            //customerDao.Update(c);

            //c = customerDao.Get("MPOLL");
            //Assert.AreEqual(c.Id, "MPOLL");
            //Assert.AreEqual(c.CompanyName, "SpringSource");

            //customerDao.Delete(c);


            //SessionFactoryUtils.GetSession(sessionFactory, true).Flush();
            //customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers");
            //Assert.AreEqual(92, customerCount);

            //try
            //{
            //    c = customerDao.Get("MPOLL");
            //    Assert.Fail("Should have thrown HibernateObjectRetrievalFailureException when finding customer with Id = MPOLL");
            //}
            //catch (HibernateObjectRetrievalFailureException e)
            //{
            //    Assert.AreEqual("Customer", e.PersistentClassName);
            //}

        }

    }
}
